package sjsu.android;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import dbAdapter.CreateFoodData;
import dbAdapter.FoodDBAdapter;
import java.util.Arrays;
public class Nutrition extends Activity {
	FoodDBAdapter fd;
	// define  adapter and view as fields to be initialized later
	int bval = 0,sval =0,lval=0, totalCalConsumed = 0;
	ListView blist = null;
	Cursor cursor;
	String item;
	String[] itemList,nutritionInfoList;
	String NutritionList[] = new String [17];
	private ArrayAdapter<String> badapter = null;
	EditText text2;
	public void onCreate(Bundle savedInstanceState) 
	{
		CreateFoodData cf = new CreateFoodData();
		cf.create(this);
		fd = new FoodDBAdapter(this);
		fd.open();
		cursor = fd.retrieveAllFoodNames();
		itemList =  new String[cursor.getCount()];
		int i=0;
		while( cursor.moveToNext()){
			itemList[i] = cursor.getString(1);
			System.out.println(itemList[i]);
			i++;
		}
		Arrays.sort(itemList);
		fd.close();
	    super.onCreate(savedInstanceState);
        setContentView(R.layout.nutritioninfo);
        try{
    
	        // initialize the adapter with the ListView defined in dataview.xml
        	// and the array of String items defined in code
        		badapter = new ArrayAdapter<String>(this, R.layout.dataview, itemList);
	      	       
	        // initializing the list from main.xml
        		blist = (ListView) this.findViewById(R.id.ListView01);
	        // getting the list view to use this adapter
        		blist.setAdapter(badapter);   
        		blist.setOnItemClickListener(new ListItemSelectedHandler());
        	}catch (Exception e){}
        	
	}
        	
   	private class ListItemSelectedHandler implements AdapterView.OnItemClickListener
    {
      	public void onItemClick(AdapterView<?> adapt, View view, int position, long id)
        {
        	Object obj = adapt.getAdapter();
        	if(obj == badapter){
        		 item = itemList[position];
        		 Intent intent = new Intent(Nutrition.this, NutritionInfo.class);
        		 intent.putExtra("itemName",item);//selected item is passed to intent
           		 startActivity(intent); 
        	}
    	}
    }		
}//End class

           
        	
       	
        	
